<?php
/**
 * @desc 短信接口基类,本类参考ThinkPHP中的cache模块进行开发
 * @author 陈聪
 */
namespace Service;

/**
 * 短信服务基类
 */
class Sms {
    /**
     * @desc 操作句柄
     * @var string
     * @access protected
     */
    protected $handler;

    /**
     * @desc 短信连接参数
     * @var integer
     * @access protected
     */
    protected $options = array ();

    /**
     * @desc 连接短信
     * @access public
     * @param string $type 短信类型(使用第三方的名字)
     * @param array $options 配置数组
     * @return object
     */
    public function connect($type = '', $options = array()) {
        if (empty ( $type )){
            $type = C ( 'SMS_TYPE' );
        }
        $class = strpos ( $type, '\\' ) ? $type : 'Service\\Sms\\' . ucwords ( strtolower ( $type ) )."\\".ucwords ( strtolower ( $type ) );
        if (class_exists ( $class ))
            $sms = new $class ( $options );
        else
            E ( L ( '_SMS_TYPE_INVALID_' ) . ':' . $type );
        return $sms;
    }

    /**
     * @desc 取得短信接口服务类实例
     * @static
     * @access public
     * @return mixed
     */
    static function getInstance($type = '', $options = array()) {
        static $_instance = array ();
        $guid = $type . to_guid_string ( $options );
        if (! isset ( $_instance [$guid] )) {
            $obj = new Sms ();
            $_instance [$guid] = $obj->connect ( $type, $options );
        }
        return $_instance [$guid];
    }

    /**
     * @desc 查询卡信息
     * @author 陈聪
     * @param array $smsInfo 手机号码|内容|时间
     */
    public function __sendMessage($smsInfo) {
        return $this->sendMessage($smsInfo);
    }

    /**
     * @desc 查询账户余额
     * @author 陈聪
     */
    public function __queryAccountBalance(){
        return $this->queryAccountBalance();
    }

    /**
     * @desc 生成验证码
     * @author 陈聪
     * @param type $length
     * @return string $code 6位数数字
     */
    public static function getCode($length = 6){
        if(empty($length)){
            $length = 6;
        }
        $length = $length - 1;
        $startNum = pow(10, $length - 1);
        $endNum = pow(10, $length) - 1;
        $firstCode = rand(0, 9);
        $laveCode = rand($startNum,$endNum);
        $code = $firstCode.$laveCode;
        return $code;
    }

    /**
     * @desc 修改配置信息
     * @author 陈聪
     */
    public function setOptions($name, $value) {
        $this->options [$name] = $value;
    }

    /**
     * @desc 获取配置信息
     * @author 陈聪
     */
    public function getOptions($name) {
        return $this->options [$name];
    }

    /**
     * @desc get方式采集url内容
     * @author 陈聪
     * @param string $url 网址
     * @param string $referer 当前页地址
     * @param string $cookie 网址cookie信息
     * @param string $useragent 请求头信息
     * @return string $html 结果
     */
    public function curl_get($url,$referer='',$cookie='',$useragent=''){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_REFERER, $referer);
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 600);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_COOKIE,$cookie);
        $html = curl_exec($ch);
        if($html == false){
            $html = curl_error($ch);
        }
        curl_close($ch);
        return $html;
    }

    /**
     * @desc POST方式采集url内容
     * @author 陈聪
     * @param string $url 网址
     * @param array $data 参数集合
     * @param string $referer 当前页地址
     * @param string $cookie 网址cookie信息
     * @param string $useragent 请求头信息
     * @return string $html 结果
     */
    public function curl_post($url,$data,$referer='',$cookie='',$useragent=''){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_REFERER, $referer);
        curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_TIMEOUT, 600);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_COOKIE,$cookie);
        $html = curl_exec($ch);
        if($html == false){
            $html = curl_error($ch);
        }
        curl_close($ch);
        return $html;
    }

    /**
     * @desc 魔术方法
     * @author 陈聪
     */
    public function __call($method, $args) {
        // 调用缓存类型自己的方法
        if (method_exists ( $this->handler, $method )) {
            return call_user_func_array ( array (
                $this->handler,
                $method
            ), $args );
        } else {
            E ( __CLASS__ . ':' . $method . L ( '_METHOD_NOT_EXIST_' ) );
            return;
        }
    }
}